<template>
	<scroll-view @scrolltolower="getNextPageData" scroll-y="true" class="body">
		<view v-for="(item, index) in list" :key="index" @click="toDetail(item)" class="card">
			<view class="title flex-row">
				<view class="name">{{ item.name }}</view>
			</view>
			<view class="item">{{ item.questions || 0 }}道题，{{ item.minuts || 0 }}分钟</view>
			<view class="item">起始时间：{{ item.start_time }}</view>
			<view class="item">结束时间：{{ item.over_time }}</view>
			<view class="btn top" @click.stop.prevent="toPage('member?id='+item.id)">未考试人员</view>
			<view class="btn" :class="{undisabled: !item.isCan}">开始答题</view>
		</view>
		<view class="nodata">没有更多了~</view>
	</scroll-view>
</template>

<script>
export default{
	data(){
		return {
			userid: uni.getStorageSync('userid'),
			page: 1,
			total: 1,
			list: []
		}
	},
	onLoad(){
		this.getList();
	},
	methods: {
		toPage(url){
			if(url) uni.navigateTo({url});
		},
		toDetail(item){
			if(!item.isCan){
				return uni.showToast({
					icon: 'none',
					title: '请在规定的日期内答题!'
				})
			}
			
			uni.navigateTo({
				url: 'detail?id='+item.id+'&type=2'
			});
		},
		// 获取列表
		getList(){
			let _this = this;
			uni.request({
				url: this.$API.edutExamFormalList,
				data: { userid: this.userid, type: 2 },
				header: this.$API.get_head(),
				success: (res)=>{
					_this.total = res.data.total;
					
					let data = res.data.rows;
					// 判断有效性
					data = data.map(item=>{
						let startTime = new Date(item.start_time).getTime();
						let overTime = new Date(item.over_time).getTime();
						let nowTime = new Date().getTime();
						item.isCan = nowTime >= startTime && nowTime <= overTime;
						return item;
					});
					
					// 判断是第一页还是后续页
					if(_this.page == 1){
						_this.list = data;
					}else{
						_this.list = _this.list.concat(data);
					}
				}
			})
		},
		// 分页加载
		getNextPageData(){
			console.log(1);
			this.page++;
			if(this.page < this.total){
				this.getList();
			}
		}
	}
}
</script>

<style scoped>
.body{
	height: 100vh;
}
	
/*卡片*/
.card{
	margin: 20upx 20upx 0;
	border-radius: 16upx;
	background: #fff;
	padding: 14upx 20upx 8upx;
	border: 2upx solid #F6F6F6;
	box-shadow: 0 0 4upx #eee;
	position: relative;
}
.card.danger{
	background: #fff;
	margin: 0 20upx 20upx;
}
.card .title{
	margin-bottom: 6upx;
}
.card .title view{
	height: 42upx;
	font-size: 28upx;
}
.card .title view.name{
	text-indent: 20upx;
	font-weight: bold;
	position: relative;
}
.card .title view.name::before{
	content: '';
	width: 10upx;
	height: 26upx;
	background: #5F96F2;
	border-radius: 4upx;
	position: absolute;
	top: 14upx;
	left: 0;
}
.card .btn{
	padding: 0;
	color: #fff;
	width: 140upx;
	height: 48upx;
	font-size: 24upx;
	line-height: 48upx;
	border-radius: 6upx;
	text-align: center;
	background: #5F96F2;
	position: absolute;
	bottom: 20upx;
	right: 20upx;
}
.card .btn.undisabled{
	background: #999;
}
.card .btn.top{
	width: 160upx;
	background: #09BB07;
	bottom: initial;
	top: 30upx;
}
</style>
